/*
 * move.c
 *
 *  Created on: Mar 30, 2013
 *      Author: locpx
 */
#include "defines.h"
#include "extglobals.h"
#include "stdio.h"

// binary: 1111 1111 1111 1111 1111 1111 1000 0000
// hex   : ffffff80
// this constant is used to clear the 'from' bitfield of a move
// by operating moveInt & 0xffffff80
const unsigned int CLEAR_FROM = 0xffffff80;

// binary: 1111 1111 1111 1111 1100 0000 0111 1111
// hex   : ffffc07f
// this constant is used to clear the 'tosq' bitfield of a move
// by operating moveInt & ffffc07f
const unsigned int CLEAR_TOSQ = 0xffffc07f;

// binary: 1111 1111 1111 1100 0011 1111 1111 1111
// hex   : fffc3fff
// this constant is used to clear the 'piec' bitfield of a move
// by operating moveInt & fffc3fff
const unsigned int CLEAR_PIEC = 0xfffc3fff;

// binary: 1111 1111 1100 0011 1111 1111 1111 1111
// hex   : ffc3ffff
// this constant is used to clear the 'piec' bitfield of a move
// by operating moveInt & ffc3ffff
const unsigned int CLEAR_CAPT = 0xffc3ffff;

// binary: 0000 0000 0000 0000 0000 0000 0111 1111
// hex:    0000007f
const unsigned int SEVEN_RIGHT_MOST = 0x0000007f;

// binary: 0000 0000 0000 0000 0000 0000 0000 1111
// hex:    0000000f
const unsigned int FOUR_RIGHT_MOST = 0x0000000f;

void setFrom(MOVE*  m_move, unsigned int m_from)
{
	m_move->moveInt &= CLEAR_FROM;
	m_move->moveInt |= (m_from & SEVEN_RIGHT_MOST);
	return;
}

void setTosq(MOVE*  m_move, unsigned int m_tosq)
{
	m_move->moveInt &= CLEAR_TOSQ;
	m_move->moveInt |= (m_tosq & SEVEN_RIGHT_MOST) << 7;
	return;
}

void setPiec(MOVE*  m_move, unsigned int m_piec)
{
	m_move->moveInt &= CLEAR_PIEC;
	m_move->moveInt |= (m_piec & FOUR_RIGHT_MOST) << 14;
	return;
}

void setCapt(MOVE*  m_move, unsigned int m_capt)
{
	m_move->moveInt &= CLEAR_CAPT;
	m_move->moveInt |= (m_capt & FOUR_RIGHT_MOST) << 18;
	return;
}

unsigned int getFrom(MOVE  m_move)
{
	return m_move.moveInt & SEVEN_RIGHT_MOST;
}

unsigned int getTosq(MOVE  m_move)
{
	return (m_move.moveInt >> 7) & SEVEN_RIGHT_MOST;
}

unsigned int getPiec(MOVE  m_move)
{
	return (m_move.moveInt >> 14) & FOUR_RIGHT_MOST;
}

unsigned int getCapt(MOVE  m_move)
{
	return (m_move.moveInt >> 18) & FOUR_RIGHT_MOST;
}

void clear(MOVE* m_move)
{
	m_move->moveInt = 0;
}

void displayMove(MOVE move)
{
	unsigned int from, to;
	from = getFrom(move);
	to = getTosq(move);

	printf("%s%s", SQUARENAME[from], SQUARENAME[to]);
}
